package 数据结构考纲.图;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

//邻接表实现的图算法
public class ALGraph<T> {
    static class Arc {  //边节点
        int weight;
        int vex;    //尾指向的顶点

        public Arc(int weight, int vex) {
            this.weight = weight;
            this.vex = vex;
        }

        @Override
        public String toString() {
            return "Arc{" +
                    "weight=" + weight +
                    ", vex=" + vex +
                    '}';
        }
    }

    List<T> vexs;  //顶点信息
    List<List<Arc>> arc = new LinkedList<>();  //边信息

    public ALGraph(T[] vex, int[][] arc) {
        this.vexs = new LinkedList<>();
        vexs.addAll(Arrays.asList(vex));
        for (int i = 0; i < vex.length; i++) {
            this.arc.add(new LinkedList<>());
            List<Arc> list = this.arc.get(i);
            for (int j = 0; j < vex.length; j++) {
                if (arc[i][j] > 0)
                    list.add(new Arc(j, arc[i][j]));
            }
        }
    }

    @Override
    public String toString() {
        return "ALGraph{" +
                "vexs=" + vexs +
                ", arc=" + arc +
                '}';
    }

    public static void main(String[] args) {
        Integer[] vex = {1,2,3};
        int[][] arc=new int[][]{{0,1,1},{3,0,6},{2,4,0}};
        ALGraph<Integer> graph = new ALGraph<>(vex, arc);
        System.out.println(graph);
    }
}
